import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
	  {
	    path: '/',
	    name: 'home',
	    component: HomeView,
		redirect:'/userManage',
		children:[
			{
				path:'/userManage',
				name:'userManage',
				component: () => import("../views/userManage.vue"),
				redirect: '/adminManage',
				children:[
					{
						path:'/adminManage',
						name:'adminManage',
						component: () => import("../views/adminManage.vue")
					},
					{
						path:'/userForBranch',
						name:'userForBranch',
						component: () => import("../views/userForBranch.vue")
					},
				]
			},
			{
				path:'/userInformation',
				name:'userInformation',
				component: () => import("../views/userInformation.vue")
			},
			{
				path:'/branch',
				name:'branch',
				component: () => import("../views/branch.vue"),
			},
			{
				path:'/addBranch',
				name:'addBranch',
				component:()=>import("../views/addBranch.vue")
			},
			{
				path:'/updateBranch',
				name:'updateBranch',
				component:()=>import("../views/updateBranch.vue")
			},
			{
				path:'/classification',
				name:'classification',
				component: () => import("../views/classification.vue")
			},
			{
				path:'/goodsManage',
				name:'goodsManage',
				component: () => import("../views/goodsManage.vue")
			},
			{
				path:'/addGoods',
				name:'addGoods',
				component: () => import("../views/addGoods.vue")
			},
			{
				path:'/orderManage',
				name:'orderManage',
				component: () => import("../views/orderManage.vue")
			},
			{
				path:'/saleManage',
				name:'saleManage',
				component: () => import("../views/saleManage.vue")
			},
			{
				path:'/Feedback',
				name:'Feedback',
				component: () => import("../views/Feedback.vue")
			},
			{
				path:'/commentManage',
				name:'commentManage',
				component: () => import("../views/commentManage.vue")
			},
			// 门店
			{
				path:'/myCenter',
				name:'myCenter',
				component: () => import("../views/myCenter.vue")
			},
			{
				path:'/branchOrder',
				name:'branchOrder',
				component: () => import("../views/branchOrder.vue")
			},
			{
				path:'/branchSales',
				name:'branchSales',
				component: () => import("../views/branchSales.vue")
			},
			{
				path:'/user',
				name:'user',
				component: () => import("../views/user.vue")
			},
			{
				path:'/commentContent',
				name:'commentContent',
				component: () => import("../views/commentContent.vue")
			},
		]
	  },
	// 登录
	{
		path:'/login',
		name:'login',
		component: () => import("../views/Login.vue")
	}
  ]
})

//全局前置守卫；
// router.beforeEach(async (to,from)=>{
// 	if(to.name!='login' && !sessionStorage.getItem("token")){
// 		return {name:"login"}
// 	}
// 	if(!sessionStorage.getItem("token")){
// 		return 
// 	}
// 	if(router.getRoutes().length==2){
// 		let menuRes=await FindMenu({
// 			roleIds:[1]
// 		})
// 		// console.log('mes',menuRes)
// 		menuRes.forEach(first=>{
// 			// console.log("first",first)
// 			let firstMenuName=first.path
// 			firstMenuName=firstMenuName.substring(1);//一级菜单名称
// 			router.addRoute('home',{
// 				name:firstMenuName,
// 				path:first.path,
// 				component:()=>import(`../views/${firstMenuName}.vue`)
// 			})
// 			// console.log(first)
// 			if(first.childs && first.childs.length!=0){
// 				// console.log(first.childs)
				
// 				first.childs.forEach(second=>{
// 					let secondMenuName=second.path.split("/")
// 					// console.log(secondMenuName[secondMenuName.length-1])
// 					secondMenuName=secondMenuName[secondMenuName.length-1]
// 					var routeObj={
// 						name:firstMenuName+"_"+secondMenuName,
// 						path:secondMenuName,
// 						component:()=>import(`../views/${secondMenuName}.vue`),
// 					}
// 					//console.log('obj',routeObj)
// 					router.addRoute('home',routeObj)
// 				})
// 			}
// 		})
// 		// console.log('getRoutes',router.getRoutes())
// 		router.replace(to.path)
// 	}
// })

export default router
